//
//  HSYShopPhotosViewController.m
//  HSY
//
//  Created by 陈浩 on 2017/4/6.
//  Copyright © 2017年 金开门. All rights reserved.
//

#import "HSYShopPhotosViewController.h"
#import "HSYUploadShopPhotosRequest.h"
#import "UIImagePickerController+Block.h"
#import "UIImage+Tools.h"
#import "BQActivityView.h"

@interface HSYShopPhotosViewController ()


@property (weak, nonatomic) IBOutlet UIImageView *shopPhotoImageView; //店铺照片
@property (weak, nonatomic) IBOutlet UIImageView *cashierPhotoImageView; //收银员与收银台照片
@property (weak, nonatomic) IBOutlet UIImageView *corporateIdentityPhotoImageView; //店铺内景照片

@property (weak, nonatomic) IBOutlet UIButton *shopPhotoBtn;
@property (weak, nonatomic) IBOutlet UIButton *cashierPhotoBtn;
@property (weak, nonatomic) IBOutlet UIButton *corporateIdentityPhoto;

@end

@implementation HSYShopPhotosViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.leftAreaLBtn setImage:[UIImage imageNamed:@"left"]  forState:UIControlStateNormal];
    [self.titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.titleButton setTitle:@"填写店铺资料" forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

- (void)setupNavigationBarBackgroundImage{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)]  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
}


-(void)handleGoBackEvent:(UIButton *)sender{
    if (self.navigationController.childViewControllers.count <= 1) {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }else{
        [super handleGoBackEvent:sender];
    }
}


- (IBAction)nextStepBtnClick:(id)sender {
    [self.view endEditing:YES];

    if (![self checkDataIntegrity]) return; //检查信息是否完善
    
    HSYUploadShopPhotosRequest *task = [[HSYUploadShopPhotosRequest alloc] init];
    task.shopId = [HSYUserCenter instatce].appBizShop.shopID;
    task.fileA = UIImageJPEGRepresentation(self.shopPhotoImageView.image, 0.1);
    task.fileB = UIImageJPEGRepresentation(self.cashierPhotoImageView.image, 0.1);
    task.fileC = UIImageJPEGRepresentation(self.corporateIdentityPhotoImageView.image, 0.1);

    [BQActivityView showActiviTy];
    __weak typeof(self)weakSelf = self;
    
    [HSYNetworkManager POST:task success:^(HSYResponse *response) {
        [BQActivityView hideActiviTy];

        if (response.resultCode == HSY_REQUEST_CODE_SUCCESS) {
            //保存步骤信息
            [HSYUserCenter instatce].appAuUser.auStep = 2;
            [[HSYUserCenter instatce] saveUserModelToDocuments];
            
            //push to 填写店铺资料
            [weakSelf pushToCorporateInformation];
        }else{
            [weakSelf showMessage:response.resultMessage];
        }
    } failure:^(NSError *error) {
        [BQActivityView hideActiviTy];
        if (error.code != NSURLErrorCancelled) {
            [weakSelf showMessage:@"请求超时"];
        }
    }];
}

//检查信息完善与否
-(BOOL)checkDataIntegrity{
    if (!self.shopPhotoImageView.image) {
        [self showMessage:@"请拍摄门头照"];
        return NO;
    }
    
    if (!self.cashierPhotoImageView.image) {
        [self showMessage:@"请拍摄收银台照片"];
        return NO;
    }
    
    
    if (!self.corporateIdentityPhotoImageView.image) {
        [self showMessage:@"请拍摄店铺内景照片"];
        return NO;
    }
    
    return YES;
}


- (IBAction)shopPhotoBtnClick:(UIButton *)sender {
    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        wSelf.shopPhotoImageView.image = image ;
        [wSelf.shopPhotoBtn setImage:nil forState:UIControlStateNormal];
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

- (IBAction)cashierPhotoBtnClick:(UIButton *)sender {
    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        wSelf.cashierPhotoImageView.image = image ;
        [wSelf.cashierPhotoBtn setImage:nil forState:UIControlStateNormal];
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

- (IBAction)corporateIdentityPhotoBtnClick:(UIButton *)sender {
    
    __weak typeof(self) wSelf = self;
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] initWithSourceType:UIImagePickerControllerSourceTypeCamera finish:^(UIImagePickerController *vc, UIImage *image) {
        wSelf.corporateIdentityPhotoImageView.image = image ;
        [wSelf.corporateIdentityPhoto setImage:nil forState:UIControlStateNormal];
    }];
    
    [self presentViewController:imagePickerVC animated:YES completion:nil];
}

#pragma mark - push to 填写店铺资料 -
//公司信息
- (void)pushToCorporateInformation{
    [HSYGoToAuditUtil goAuditFromViewController:self push:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
